package com.tanhua.api.impl.mongo;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.api.mongo.FriendsApi;
import com.tanhua.api.mongo.RecommendUserApi;
import com.tanhua.api.mongo.UserLikeApi;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.UserLove;
import com.tanhua.domain.mongo.UserNlove;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.mapper.db.UserInfoMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserLikeApi userLikeApi;

    @Autowired
    private FriendsApi friendsApi;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public RecommendUser findBytodayBest(Long userid) {

        Query query = new Query(Criteria.where("toUserId").is(userid));
        query.with(new Sort(Sort.Direction.DESC, "score"));
        query.skip(0).limit(1); //只拿一条数据
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser; //最高缘分值 女神id
    }

    @Override
    public PageBeanVo findRecommendation(Integer page, Integer pagesize, Long userid) {

        Query query = new Query(Criteria.where("toUserId").is(userid));
        query.with(new Sort(Sort.Direction.DESC, "score"));
        query.skip((page - 1) * pagesize).limit(pagesize);
        long count = mongoTemplate.count(query, RecommendUser.class);
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);

        PageBeanVo pageBeanVo = new PageBeanVo(page, pagesize, count, list);

        return pageBeanVo;
    }

    @Override
    public RecommendUser findScore(Integer jrid, Long id) {
        Query query = new Query(Criteria.where("userId").is(jrid).and("toUserId").is(id));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }


    @Override
    public HashSet<Long> findNoShowId(Long userId) {

        HashSet<Long> userIds = new HashSet<>();//后期测试数据用List集合时有重复ID，所以这里直接改为Set，避免重复
        //查询喜欢列表和好友的列表用户的ID
        List<Long> friendsId = friendsApi.findFriendsId(userId);
        userIds.addAll(friendsId);
        List<Long> userLikeId = userLikeApi.findUserLikeId(userId);
        userIds.addAll(userLikeId);
        //查询我已经点过喜欢的用户的ID
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserLove> userLoves = mongoTemplate.find(query, UserLove.class);
        for (UserLove userLove : userLoves) {
            userIds.add(userLove.getLoveUserId());
        }
        //查询我不喜欢的用户的ID
        List<UserNlove> userNloves = mongoTemplate.find(query, UserNlove.class);
        for (UserNlove userNlove : userNloves) {
            userIds.add(userNlove.getNloveUserId());
        }

        //查询粉丝的ID
        Query query3 = new Query(Criteria.where("likeUserId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query3, UserLike.class);
        for (UserLike userLike : userLikes) {
            userIds.add(userLike.getUserId());
        }
        return userIds;

    }

    @Override
    public List<RecommendUser> queryCardList(Long userId, int count) {
        //先查询MongoDB中的推荐数据，此数据优先于随机查询的数据，后续可以对接大数据。
        PageRequest pageRequest = PageRequest.of(0, count, Sort.by(Sort.Order.desc("score")));
        HashSet<Long> userIds = findNoShowId(userId);

        //查询除了朋友，喜欢的人，我不喜欢的之外的推荐人
        Criteria criteria = Criteria.where("toUserId").is(userId).and("");
        if (CollUtil.isNotEmpty(userIds)) {
            criteria.andOperator(Criteria.where("userId").nin(userIds));
        }

        Query query = Query.query(criteria).with(pageRequest);
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        return recommendUserList;
    }

    @Override
    public ArrayList<Integer> SelectRandomUserId(HashSet<Long> userIds, int count,String gender) {

        //生成mysql查询的String数据
        //这里查询的是除了朋友，已经喜欢的，和我曾经不喜欢的之外的用户的随机ID
        String a = new String();
        for (Long userId : userIds) {
            a = a + userId + ",";
        }
        if (userIds.size()!=0){
            String substring = a.substring(0,a.length() - 1);
            ArrayList<Integer> RandomUserId = userInfoMapper.RandomUserId(substring, count,gender);
            System.out.println(RandomUserId.size());
            System.out.println(RandomUserId);
            return RandomUserId;
        }

        ArrayList<Integer> RandomUserId = userInfoMapper.RandomUserId("0", count,gender);
        return RandomUserId;

    }
}



